<template>
  <view class="empty">
    <image src="../../image/notPhone.png" class="empty-img1" />
    <view class="empty-p1">请获取手机号后查看</view>
    <button class="btn-submit" @tap="wx_login()">授权获取手机</button>
  </view>
</template>
<script>
import "./authorize.css";
import { setGlobalData, getGlobalData } from "../../globalData.js";
import bulterApi from "@souche2014/bulter-api";
const api = bulterApi({ partner: "IOParking" });

export default {
  data() {
    return {
      phoneNumber: null, //用户手机号
      code: null, //微信登陆 code
      iv: null, //解密算法 iv
      sessionId: null, //解密算法 sessionId
      encryptedData: null, //解密数据 encryptedData
      openId: null, //用户唯一标识
      unionId: null, //小程序标识
      hasUserInfo: true, //是否授权获取微信基础数据
      isPhoneNmu: true, //是否授权获取手机号
      apiBaseUrl: process.env.IOParking.API_HOST //请求头
    };
  },
  onLoad: function(options) {},
  methods: {
    wx_login() {
      //获取用户信息
      api
        .getUserInfo({
          scope: "all"
        })
        .then(resp => {
          console.log(resp)
          if (resp.success) {
            console.log("authCode is ", resp.data);
          } else {
            console.log("err is ", resp.message, "err code is ", resp.code);
          }
        });
    },
    // wx_login() {
    //   //微信登陆接口 获取code
    //   var that = this;
    //   wx.login({
    //     success(res) {
    //       console.log("微信登陆接口 获取code", res);
    //       if (res.code) {
    //         //获取 sessionId openId
    //         that.code = res.code;
    //         that.getSessionId();
    //       }
    //     }
    //   });
    // },
    getSessionId() {
      //后台登陆获取 sessionId openId unionId
      Taro.showLoading({
        title: "加载中"
      });
      var base = this.apiBaseUrl;
      var that = this;
      wx.request({
        url:
          base +
          "/weixin/login?code=" +
          that.code +
          "&appid=wxd38e05a1db7120e1",
        data: {
          code: that.code,
          appid: "wxd38e05a1db7120e1" //小程序唯一ID
        },
        header: {
          "content-type": "application/json" // 默认值
        },
        method: "GET",
        success(res) {
          console.log("后台登陆获取:", res.data);
          if (res.data.data.openId) {
            Taro.hideLoading();
            setGlobalData("openId", res.data.data.openId);
            that.qrCodeUrl();
          }
        }
      });
    },
    getPhoneNumber(e) {
      //获取微信绑定手机号加密数据
      if (e.detail.iv) {
        this.setData({
          iv: e.detail.iv,
          encryptedData: e.detail.encryptedData
        });
        this.decryptPhoneNumber();
      }
    },
    decryptPhoneNumber() {
      //***************解密手机号 并缓存基础数据***********
      var that = this;
      var base = this.data.apiBaseUrl;
      wx.request({
        url: base + "/weixin/decryptphonenumber",
        data: {
          sessionKey: that.data.sessionId,
          encryptedData: that.data.encryptedData,
          iv: that.data.iv
        },
        header: {
          "content-type": "application/json" // 默认值
        },
        method: "POST",
        success(res) {
          console.log("解密手机号：", res.data);
          if (res.data.isSuccess) {
            //****************缓存用户数据******
            var local = {
              phoneNumber: res.data.data.phoneNumber
              // nickName: that.data.userInfo.nickName,
              // avatarUrl: that.data.userInfo.avatarUrl,
            };
            console.log(local);
            wx.setStorage({
              key: "_ild_user_" + app.globalData.openId,
              data: JSON.stringify(local)
            });
            // wx.setStorage({
            //   key: 'parkingUserInfo',
            //   data: JSON.stringify(local)
            // })
            that.setData({
              isPhoneNmu: true,
              phoneNumber: res.data.data.phoneNumber
            });
            app.globalData.isPhoneNmu = true;
            app.globalData.phoneNumber = res.data.data.phoneNumber;
            that.initData();
            //********************* 后台注册微信用户
            that.regUser();
          }
        }
      });
    },
    regUser() {
      //*********************后台注册微信用户
      var that = this;
      var base = this.data.apiBaseUrl;
      wx.request({
        url: base + "/weixin/register",
        data: {
          id: 0,
          openID: app.globalData.openId,
          unionID: that.data.unionId || "",
          nickName: that.data.userInfo.nickName || "",
          avatarUrl: that.data.userInfo.avatarUrl || "",
          gender: that.data.userInfo.gender,
          mobile: app.globalData.phoneNumber,
          appId: "wxd38e05a1db7120e1"
        },
        header: {
          "content-type": "application/json" // 默认值
        },
        method: "POST",
        success(res) {
          console.log(res);
          if (res.data.isSuccess) {
            wx.redirectTo({
              url: "../cardManage/card"
            });
          }
        }
      });
    }
  }
};
</script>